package com.moyu.shopapp.controller;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.moyu.shopapp.entity.Order;
import com.moyu.shopapp.entity.vo.CartGoodsVO;
import com.moyu.shopapp.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@CrossOrigin
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 添加订单
     * @param token 用户token
     * @param totalPrice 订单总价
     * @param cartListJson 货物json数据
     * @return
     */
    @PostMapping("/addOrder")
    public String addOrder(String token, double totalPrice,String cartListJson){
        Gson gson = new Gson();
        TypeToken<List<CartGoodsVO>> rule = new TypeToken<List<CartGoodsVO>>() {};
        List<CartGoodsVO>  cartlist = gson.fromJson(cartListJson, rule.getType());

        return orderService.addOrder(token,totalPrice,cartlist);
    }

    /**
     * 取消订单
     * @param token 用户token
     * @param orderId 订单id
     * @return
     */
    @PostMapping("/cancelOrder")
    public String cancelOrder(String token,int orderId){
        return orderService.cancelOrder(token,orderId);
    }

    /**
     * 获取订单详细
     * @param orderId 订单id
     * @return
     */
    @GetMapping("/findOrderById")
    public Order findOrderById(int orderId){
        return orderService.getById(orderId);
    }

    /**
     * 获取单个用户订单数据
     * @param token 用户token
     * @return
     */
    @GetMapping("/findOrderByUserId")
    public List<Order> findOrderByUserId(String token){
        return orderService.getOrderByUserId(token);
    }

    /**
     * 手动更新订单状态
     * @param orderId 订单id
     * @return
     */
    @PostMapping("/updateOrderStatus")
    public String  updateOrderStatus(int orderId){
        return orderService.updateOrderStatus(orderId);
    }

}
